package com.logic.landseaserver.ws;

import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.logic.common.enums.HttpCodeEnum;
import com.logic.common.exception.BusinessException;
import com.logic.common.util.CommonResult;
import com.logic.common.util.CommonResultMap;
import com.logic.common.util.QueryUtil;
import com.logic.landseaserver.domain.EntityResponse;
import com.logic.landseaserver.service.IEntityResponseService;
import com.logic.landseaserver.service.utils.ResetQueryUtil;
import com.logic.landseaserver.ws.dto.EntityResponseDTO;
import com.logic.landseaserver.ws.dto.ProjectCheckDTO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;


@Path("/entityResponse")
@Api(value="entityResponse")
public class EntityResponseRest {
	@Autowired
	private IEntityResponseService entityResponseServiceImpl;

	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@POST
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	@ApiOperation(value = "Create a new entityResponse")
	public Response create(@ApiParam(value = "param", required = true) EntityResponse entityResponse) {
		CommonResultMap map = new CommonResultMap();
        try {
        	int id = entityResponseServiceImpl.insert(entityResponse);
        	entityResponse.setId(id);
            map.setData(entityResponseServiceImpl.getDTO(id));
            map.setResult(CommonResult.SUCCESS, "Create success");
        } catch (BusinessException e) {
            logger.error("create error.", e);
            map.setResult(e);
        } catch (Exception e) {
            logger.error("create error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
	}


	@DELETE
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "logic delete entityResponse by set deleted")
    public Response logicDelete(@ApiParam(value = "param", required = true) @QueryParam("entityResponseId") String id) {
        CommonResultMap map = new CommonResultMap();
        try {
        	entityResponseServiceImpl.delete(id);
            map.setResult(CommonResult.SUCCESS,"delete entityResponse");
        } catch (BusinessException e) {
            logger.error("logicDelete error.", e);
            map.setResult(e);
        }  catch (Exception e) {
            logger.error("logicDelete error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

	@PUT
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	@ApiOperation(value = "edit entityResponse ")
    public Response update(@ApiParam(value = "param", required = true) EntityResponse entityResponse) {
        CommonResultMap map = new CommonResultMap();
        try {
        	entityResponseServiceImpl.update(entityResponse);
            map.setResult(CommonResult.SUCCESS,"edit entityResponse");
        } catch (BusinessException e) {
            logger.error("update error.", e);
            map.setResult(e);
        }  catch (Exception e) {
            logger.error("update error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }


    @GET
    @Path("/detail/{entityResponseId}")
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "Get entityResponse by entityResponse id")
    public Response retrieveDTO(
            @ApiParam(value = "param", required = true) @PathParam("entityResponseId") Integer entityResponseId) {
        CommonResultMap map = new CommonResultMap();
        try {
            map.setData(entityResponseServiceImpl.getDTO(entityResponseId));
            map.setResult(CommonResult.SUCCESS);
        } catch (Exception e) {
            logger.error("retrieveDTO error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }


	@POST
	@Path("/detail/list")
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	@ApiOperation(value = "list entityResponses")
	public Response listDTO(@ApiParam(value = "param", required = true) QueryUtil queryUtil){
        CommonResultMap map = new CommonResultMap();
        List<EntityResponseDTO> entityResponseList = null;
        try {
            queryUtil.getPagingTool().calculateStartIndex();
            ResetQueryUtil.reset(queryUtil);
            entityResponseList = entityResponseServiceImpl.queryDTO(queryUtil);
            Integer totalCount = queryUtil.getPagingTool().getTotalNum();
            map.setData(entityResponseList);
            map.setResultTotalCount(totalCount);
            map.setResult(CommonResult.SUCCESS);

        } catch (Exception e) {
            logger.error("listDTO error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
	}

	@GET
	@Path("/detail/list/{issueId}")
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	@ApiOperation(value = "list entityResponses")
	public Response listDTOByIssueId(@ApiParam(value = "param", required = true) @PathParam("issueId") Integer issueId){
        CommonResultMap map = new CommonResultMap();
        List<EntityResponseDTO> entityResponseList = null;
        try {
            entityResponseList = entityResponseServiceImpl.queryDTOByIssueId(issueId);
            map.setData(entityResponseList);
            map.setResult(CommonResult.SUCCESS);

        } catch (Exception e) {
            logger.error("listDTOByIssueId error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
	}

	@GET
	@Path("/detail/listIssues/{checkId}")
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	@ApiOperation(value = "list entityResponses")
	public Response listProjectCheckDTOByCheckId(@ApiParam(value = "param", required = true) @PathParam("checkId") Integer checkId){
        CommonResultMap map = new CommonResultMap();
        ProjectCheckDTO entityResponseList = null;
        try {
            entityResponseList = entityResponseServiceImpl.queryProjectCheckDTOByCheckId(checkId);
            map.setData(entityResponseList);
            map.setResult(CommonResult.SUCCESS);

        } catch (Exception e) {
            logger.error("listProjectCheckDTOByCheckId error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
	}

	@GET
	@Path("/detail/list/projectId/{projectId}")
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	@ApiOperation(value = "list entityResponses")
	public Response listDTOByProjectId(@ApiParam(value = "param", required = true) @PathParam("projectId") Integer projectId){
        CommonResultMap map = new CommonResultMap();
        List<EntityResponseDTO> entityResponseList = null;
        try {
            entityResponseList = entityResponseServiceImpl.queryDTOByProjectId(projectId);
            map.setData(entityResponseList);
            map.setResult(CommonResult.SUCCESS);

        } catch (Exception e) {
            logger.error("listDTOByProjectId error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
	}

	@GET
	@Path("/detail/list/checkId/{checkId}")
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	@ApiOperation(value = "list entityResponses")
	public Response listDTOByCheckId(@ApiParam(value = "param", required = true) @PathParam("checkId") Integer checkId){
        CommonResultMap map = new CommonResultMap();
        List<EntityResponseDTO> entityResponseList = null;
        try {
            entityResponseList = entityResponseServiceImpl.queryDTOByCheckId(checkId);
            map.setData(entityResponseList);
            map.setResult(CommonResult.SUCCESS);

        } catch (Exception e) {
            logger.error("listDTOByCheckId error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
	}

}
